##make the figures of having a daughter and gender
library(tidyverse)
library(Rmisc)
limited<-limited%>%
  mutate(treatmentD = paste('treatment2', 'daughter', sep = "-"))

limited$treatmentD<-interaction(limited$treatment2, limited$daughter)

table(limited$treatmentD)

limited_D<-limited%>%
  filter(daughter==1)

limited_ND<-limited%>%
  filter(daughter==0)

AUTH_mean_D <- summarySE(limited_D, measurevar="Author", groupvars="treatment",
                         na.rm=TRUE, conf.interval=.95)
AUTH_mean_D$test<-c("Control", "Boy", "Girl")
AUTH_mean_D


limited_ND<-limited%>%
  filter(daughter==0)

AUTH_mean_ND <- summarySE(limited_ND, measurevar="Author", groupvars="treatment",
                          na.rm=TRUE, conf.interval=.95)
AUTH_mean_ND$test<-c("Control", "Boy", "Girl")
AUTH_mean_ND

AUTH_mean_D$Author2<-AUTH_mean_ND$Author
AUTH_mean_D$ci2<-AUTH_mean_ND$ci


daughter<-ggplot(AUTH_mean_D, aes(x=factor(test, level=
                                             c("Control", "Boy", "Girl")), 
                                  y=Author, group=1)) +
  geom_line() +
  geom_line(data = AUTH_mean_ND, aes(x = factor(test, level=
                                                  c("Control", "Boy", "Girl")), y = Author), color = "blue") +
  geom_errorbar(width=.1, aes(ymin=Author-ci, ymax=Author+ci), colour="red", data=AUTH_mean_D) +
  geom_errorbar(width=.1, aes(ymin=Author-ci, ymax=Author+ci), colour="red", data=AUTH_mean_ND) +
  geom_point(shape=21, size=1, fill="white")+
  geom_point(shape=21, size=1, fill="white")+
  xlab("Treatment")+
  ylab("Proportion choosing \n'Respect for Elders'")+
  ylim(0,5)+ theme_bw()

daughter





daughter<-ggplot()+
  #with a daughter
  geom_line(data=AUTH_mean_D, mapping = (aes(x=factor(test, level=
                                                        c("Control", "Boy", "Girl")), y=Author, group=1)))+ 
  
  geom_point(data=AUTH_mean_D, mapping = (aes(x=factor(test, level=
                                                         c("Control", "Boy", "Girl")), y=Author, group=1)))+
  geom_errorbar(width=.1, aes(y=Author, 
                              x=factor(test, level=
                                         c("Control", "Boy", "Girl")),
                              ymin=Author-ci, ymax=Author+ci), colour="Black", data=AUTH_mean_D) +
  #without a daughter
  geom_line(data=AUTH_mean_D, mapping = (aes(x=factor(test, level=
                                                        c("Control", "Boy", "Girl")), y=Author2, group=1, color="red")))+ 
  
  geom_point(data=AUTH_mean_D, mapping = (aes(x=factor(test, level=
                                                         c("Control", "Boy", "Girl")), y=Author2, group=1, color="red")))+
  geom_errorbar(width=.1, aes(y=Author2, 
                              x=factor(test, level=
                                         c("Control", "Boy", "Girl")),
                              ymin=Author2-ci2, ymax=Author2+ci2), colour="red", data=AUTH_mean_D) +
  
  xlab("Treatment")+
  ylab("Authoritarianism \n(Respondents with a daughter in black, without in red)")+
  ylim(0,5)+ theme_bw()+theme(legend.position="none")
daughter



limited_M<-limited%>%
  filter(Male==1)

limited_NM<-limited%>%
  filter(Male==0)

AUTH_mean_M <- summarySE(limited_M, measurevar="Author", groupvars="treatment",
                         na.rm=TRUE, conf.interval=.95)
AUTH_mean_M$test<-c("Control", "Boy", "Girl")
AUTH_mean_M


limited_NM<-limited%>%
  filter(Male==0)

AUTH_mean_NM <- summarySE(limited_NM, measurevar="Author", groupvars="treatment",
                          na.rm=TRUE, conf.interval=.95)
AUTH_mean_NM$test<-c("Control", "Boy", "Girl")
AUTH_mean_NM

AUTH_mean_M$Author2<-AUTH_mean_NM$Author
AUTH_mean_M$ci2<-AUTH_mean_NM$ci

gender<-ggplot()+
  #Male
  geom_line(data=AUTH_mean_M, mapping = (aes(x=factor(test, level=
                                                        c("Control", "Boy", "Girl")), y=Author, group=1)))+ 
  
  geom_point(data=AUTH_mean_M, mapping = (aes(x=factor(test, level=
                                                         c("Control", "Boy", "Girl")), y=Author, group=1)))+
  geom_errorbar(width=.1, aes(y=Author, 
                              x=factor(test, level=
                                         c("Control", "Boy", "Girl")),
                              ymin=Author-ci, ymax=Author+ci), colour="Black", data=AUTH_mean_M) +
  #Note Male
  geom_line(data=AUTH_mean_M, mapping = (aes(x=factor(test, level=
                                                        c("Control", "Boy", "Girl")), y=Author2, group=1, color="red")))+ 
  
  geom_point(data=AUTH_mean_M, mapping = (aes(x=factor(test, level=
                                                         c("Control", "Boy", "Girl")), y=Author2, group=1, color="red")))+
  geom_errorbar(width=.1, aes(y=Author2, 
                              x=factor(test, level=
                                         c("Control", "Boy", "Girl")),
                              ymin=Author2-ci2, ymax=Author2+ci2), colour="red", data=AUTH_mean_M) +
  
  xlab("Treatment")+
  ylab("Authoritarianism \n(Male respondents in black, female respondents in red)")+
  ylim(0,5)+ theme_bw()+theme(legend.position="none")
gender

library(cowplot)
pdf(file="Figure7.pdf")

plot_grid(daughter, gender)
dev.off()

